'use strict';


const Service = require('egg').Service;
const sequelize = require('sequelize');
const vlan = require('./olt');
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT');
const XDOLT = require('../oltmodules/XinDeOLT');
const XGLOLT = require('../oltmodules/XinGeLinOLT');
const ulid = require('../public/ulid');
class VlanService extends Service {
  async list(params) {
    let result = {
      count:"",
      rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.search){
      where.VlanID = new RegExp(params.search)
    }
    result.count = await this.ctx.model.Vlan.find(where).count('_id')
    result.rows = await this.ctx.model.Vlan.find(where).collation({"locale": "zh", numericOrdering:true}).sort({VlanID:1}).skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async create(params) {
    const result = {
      msg: '',
      flag:false
    };
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      let vlanlist = await this.ctx.model.Vlan.find({oltId:params.oltId,VlanID:params.vlanId})
      if(vlanlist.length != 0){
        result.msg = "create vlan fail,the vlan is exits"
        result.flag = false
        return result
      }
      await createVlan(params.vlanId,oltResult,this)
      await this.ctx.model.Vlan.create({_id:ulid.ulid(),oltId:params.oltId,VlanID:params.vlanId,description:params.description})
      result.msg = "create vlan successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "create vlan fail,"+error
      result.flag = false
      return result
    }
  }
  async delete(params) {
    const result = {
      msg: '',
      flag:false
    };
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      await deleteVlan(params.vlanId,oltResult,this)
      await this.ctx.model.Vlan.deleteOne({_id:params.id})
      result.msg = 'delete vlan successful';
      result.flag = true
      return result;
    } catch (error) {
      result.msg = 'delete vlan fail,' + error;
      result.flag = false
      return result;
    }
  }
  async edit(params) {
    const result = {
      msg: '',
      flag:false
    };
    try {
      await this.ctx.model.Vlan.updateOne({_id:params.id},{$set:{description:params.description}})
      result.msg = 'edit vlan successful';
      result.flag = true
      return result;
    } catch (error) {
      result.msg = 'edit vlan fail,' + error;
      result.flag = false
      return result;
    }
  }
  async listAll(params) {
    const result = await this.ctx.model.Vlan.find({ oltId: params.OltId }).collation({"locale": "zh", numericOrdering:true}).sort({VlanID:1})
    return result
  }
  async vlanverify(params) {
    const result = {
      msg: '',
      flag: false,
    };
    const vlanResult = await this.ctx.model.Vlan.findOne({ where: { oltId: params.oltId, VlanID: params.VlanID } });
    if (!vlanResult) {
      // 不存在
      result.msg = 'successful';
      result.flag = true;
      return result;
    }
    // 存在
    result.msg = 'A VLAN with the same ID already exists.';
    result.flag = false;
    return result;

  }
}
async function createVlan(vlanId,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.setVlan(vlanId,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.setVlan(vlanId,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function deleteVlan(vlanId,oltInformation,me){
  const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
  let result
  try {
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.deleteVlan(vlanId,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        const hw = new HWOLT(options)
        result = await hw.deleteVlan(vlanId,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}

module.exports = VlanService;
